from ast import arg
from operator import truediv
from re import T
import threading
from agent import Agent
from env import Env
import csv


def readCSV(filename, type):
    '''
        读csv文件
    '''
    arr, tmp = [], []
    with open(filename, 'r') as f:
        for line in f:
            linelist = line.split(',')
            for index, item in enumerate(linelist):
                if type == 'float':
                    tmp.append(float(item))
                elif type == 'int':
                    tmp.append(int(item))
                else:
                    return []
            arr.append(tmp)
            tmp = []
        return arr


if __name__ == '__main__':

    # 初始化maze矩阵
    maze = readCSV('maze_map.csv', 'int')
    # 初始化reward矩阵
    reward1 = readCSV('reward1.csv', 'float')
    reward2 = readCSV('reward2.csv', 'float')
    # 迷宫尺寸
    size = len(maze)

    # 初始化qtable为0
    qtable1 = [[[0 for x in range(4)] for y in range(size)]
               for z in range(size)]

    qtable2 = [[[0 for x in range(4)] for y in range(size)]
               for z in range(size)]

    maze[0][size-1] = 2
    env = Env(maze)

    agent1 = Agent('t1', qtable1, reward1, env, (0, 0), 0.1, 0.7)
    agent2 = Agent('t2', qtable2, reward2, env, (0, size-1), 0.15, 0.65)

    loops = 200

    t1 = threading.Thread(target=agent1.train, args=[loops])
    t2 = threading.Thread(target=agent2.train, args=[loops])

    agent1.env.gui.renderMaze()
    agent2.env.gui.renderMaze()

    t1.start()
    t2.start()

    agent1.env.gui.mainloop()
    agent2.env.gui.mainloop()

    t1.join()
    t2.join()
